struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};


class Solution {
    int Count = 0;
    void _countNodes(TreeNode* root)
    {
        if (root == nullptr) return;
        Count++;
        _countNodes(root->left);
        _countNodes(root->right);
    }
public:
    int countNodes(TreeNode* root) {
        _countNodes(root);
        return Count;
    }
};